package com.etboot.modules.base.serviceimpl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.etboot.common.vo.SearchVo;
import com.etboot.modules.base.dao.mapper.MessageMapper;
import com.etboot.modules.base.entity.Message;
import com.etboot.modules.base.service.MessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 消息内容接口实现
 * @author xboot
 */
@Slf4j
@Service
@Transactional
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements MessageService {
    @Autowired
    private MessageMapper messageMapper;

    @Override
    public Page<Message> findByCondition(Message message, SearchVo searchVo, Page pageable) {
        QueryWrapper<Message> qw = new QueryWrapper<>();

        // 模糊搜素
        if(StrUtil.isNotBlank(message.getTitle())){
            qw.like("title",message.getTitle());
        }
        if(StrUtil.isNotBlank(message.getContent())){
            qw.like("content",message.getContent());
        }

        if(message.getType()!=null){
            qw.like("type",message.getType());
        }
        // 创建时间
        if(StrUtil.isNotBlank(searchVo.getStartDate())&&StrUtil.isNotBlank(searchVo.getEndDate())){
            Date start = DateUtil.parse(searchVo.getStartDate());
            Date end = DateUtil.parse(searchVo.getEndDate());
            qw.between("create_time",start,DateUtil.endOfDay(end));
        }

        return messageMapper.selectPage(pageable,qw);
    }

    @Override
    public List<Message> findByCreateSend(Boolean createSend) {
        return messageMapper.selectList(Wrappers.<Message>lambdaQuery().eq(Message::getCreateSend,createSend));
    }
}